package cn.tedu;

import com.alibaba.druid.pool.DruidDataSource;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Author：程Sir
 * Name： SpringTest01
 * Content:
 */
@Configuration // 表示是spring的配置类
@ComponentScan("cn.tedu") // 使用组件扫描的形式进Bean的管理
@PropertySource("classpath:jdbc.properties")
public class SpringConfig {
//    @Value(value = "${jdbc.driver}") // 在注解参数的大括号中的值，是jdbc.properties文件中等号左侧的值
//    private String driver;
    @Value(value = "${jdbc.url}")
    private String url;
    @Value(value = "${db.username}")
    private String username;
    @Value(value = "${db.password}")
    private String password;
    @Value("${db.maxActive}")
    private Integer maxActive;
    @Value("${db.initialSize}")
    private Integer initialSize;

    @Bean
    public Connection connection() throws SQLException {
        Connection connection = DriverManager.getConnection(url,username,password);
        return connection;
    }

    @Bean
    public DruidDataSource druidDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        // 设置连接池的必要参数
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxActive(maxActive);
        dataSource.setInitialSize(initialSize);

        return dataSource;
    }


}
